// 用法如下:
function fn1 (x) {
  return x + 1;
}
function fn2 (x) {
  return x + 2;
}
function fn3 (x) {
  return x + 3;
}
function fn4 (x) {
  return x + 4;
}

function compose (...funcs) {
  if (funcs.length === 0) {
    return (v) => v
  }
  if (funcs.length === 1) {
    return funcs[0]
  }
  return funcs.reduceRight((a, b) => {
    return (v) => b(a(v))
  })
}


const a = compose(fn1, fn2, fn3, fn4);
// console.log(a.toString())
console.log(a(1)); // 1+4+3+2+1=11
